// packageI/pages/laboratoryOrders/laboratoryOrders.js
const app = getApp()
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: applet_park_api_url_imgUrl,
    list: [],
    currentIndex: '',
    openid: wx.getStorageSync("OPENID"),
    token: wx.getStorageSync('TOKEN'),
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInfos()
  },

  getInfos(e) {
    var that = this;
    if (e) {
      that.data.currentIndex = e.target.dataset.id;
      that.setData(that.data);
    }
    wx.request({
      url: applet_park_api_url + '/api/laboratoryOrder/list',
      data: {
        status: that.data.currentIndex
      },
      header: {
        Authorization: that.data.token
      },
      success: function (res) {
        if (res.data.code == 0) {
          for(var i in res.data.data){
            res.data.data[i].laboratoryImages = res.data.data[i].laboratoryImages.split(',')[0]
          }
          that.data.list = res.data.data;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: res.data.data
          })
        }
      }
    })
  },
  goPay(e){
    var that = this
    wx.request({
      url: applet_park_api_url + '/api/laboratoryOrder/payOrder',
      header:{
        Authorization : that.data.token
      },
      data:{
        openId: that.data.openid,
        orderNo: e.currentTarget.dataset.no
      },
      success:function(res){
        if(res.data.code==0){
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.prepay_id,
            signType: 'MD5',
            paySign: res.data.data.sign,
            paySource: 1,
            success(res) {
              wx.showToast({
                title: '支付成功',
              })
              setTimeout(function () { //刷新列表
                that.getInfos()
              }, 500);
            },
            fail() {
              setTimeout(function () { //刷新列表
                that.getInfos()
              }, 500);
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})